local menu = {};
local base = Widget;
for k,v in pairs(base)do
    menu[k] = v;
end

if(ui:isPlatform("SDL2"))then
function menu.LockRender(self) end;
function menu.UnlockRender(self) end;
function menu.render(self) end;

--[[
{
    {"File",click_function,},
    {"Show",nil,
        {
            {"subMenu1",click_function2,},
            {"subMenu2",click_function3,},
        }
    },
}

]]
function menu.render(self)
    --print(self._type);
    if(self.sdl2==nil and self.menulist)then
        self:init();
    end
    draw(self.sdl2.name,self.x,self.y,self.width,self.height);
    if(self.sdl2 and self.sdl2.name and self.shown)then
        local height = 16+6;
        local width = self.width;
        if(self.selectX and self.selectY)then
            local x = self.x
            local h = self.selectY;
            local y = 0
            while(h>height)do
                y = y + height;
                h = h - height;
            end
            y = y + self.y;
            --print(x,y);
            drawRect(x,y,width,height,0xF0222222);
            x = x + 1; y = y + 1; width = width - 2; height = height - 2;
            drawRect(x,y,width,height,0xF0222222);
            --x = x + 1; y = y + 1; width = width - 2; height = height - 2;
            --drawRect(x,y,width,height,0xF0222222);
        end
        
    end
end

function menu.init(self)
    local menuList = self.menulist;
    local height = 0;
    local fontHeight = 16+6;
    local fontWidth = 16;
    local width = 0;
    height = fontHeight * #menuList;
    for k,v in ipairs(menuList) do
        --title
        if(#v[1]>=width)then
            width = #v[1];
        end
    end
    width = width* fontWidth;
    if(width<255)then
        width = 255;
    end
    self.sdl2 = {};
    self.sdl2.name = allocName("widget");
    self.width = width;
    self.height = height;
    createCanvas(self.sdl2.name,width,height);
    local last = setRenderTarget(self.sdl2.name);
    local color = 0xFFE0E0E0;
    --drawRect(0,0,width,height,0xFFDCDCDC,true);
    --drawRect(0,0,width,height,0xFFCCCCCC);

    drawRect(0,0,width,height,color,true);
    drawRect(0,0,width,height,color - 0x202020);

    for k,v in ipairs(menuList) do
        if(isstring(v[1]))then
            drawStringPos(v[1],20,(k-1)*fontHeight);
        end
    end
    --renderPresent();
    setRenderTarget(last);
end

end

PopupMenu = menu;
PopupMenu.__index = PopupMenu;



local __call = function()
    local self = base();
    self._type = "PopupMenu";
    setmetatable(self,PopupMenu);
    return self;
end

setmetatable(PopupMenu,{__call = __call});
